package org.example;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

public class demo2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException {
/*获取字段，成员变量*/
        Class clazz = Class.forName("org.example.Student");

        //获取所有成员变量
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields){
            System.out.println(field);
        }

        //获取单个成员变量
        Field name = clazz.getDeclaredField("name");
        System.out.println(name);

        //获取权限修饰符
        int modifiers = name.getModifiers();
        System.out.println(modifiers);

        //获取成员变量的名字
        String n = name.getName();
        System.out.println(n);

        Class<?> type = name.getType();
        System.out.println(type);

        //获取成员变量记录的值
       /* Student s = new Student("zhangsan",23,"男");
        name.setAccessible(true);
        String value = (String) name.get(s);
        System.out.println(value);

        //修改对象的值
        name.set(s,"lisi");
        System.out.println(s);*/
    }
}
